package com.dwjf.core.net;

import com.dwjf.core.app.ConfigType;
import com.dwjf.core.app.Latte;
import com.dwjf.core.delegates.BaseDelegate;

import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class RestCreator {

    public static final WeakHashMap<String,Object> params = new WeakHashMap<>();

    private RestService restService;

    private RestCreator(){
        String BASE_URL = (String) Latte.getConfigurations().get(ConfigType.API_HOST.name());
        OkHttpClient OK_HTTP_CLIENT = new OkHttpClient.Builder()
                .connectTimeout(2, TimeUnit.SECONDS)
                .readTimeout(2,TimeUnit.SECONDS)
                .build();
        Retrofit RETROFIT_CLIENT = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(OK_HTTP_CLIENT)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();


        restService = RETROFIT_CLIENT.create(RestService.class);
    }

    private static class Holder{
        private static final RestCreator INSTANCE = new RestCreator();
    }

    public static RestCreator getInstance(){
        return Holder.INSTANCE;
    }

    public RestService getRestService() {
        return restService;
    }
}
